//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
namespace DotNetNuke.UI.WebControls
{
	public class TextColumnTemplate : ITemplate
	{
		private ListItemType mItemType = ListItemType.Item;
		private string mDataField;
		private bool mDesignMode;
		private string mText;
		private Unit mWidth;
		public TextColumnTemplate() : this(ListItemType.Item)
		{
		}
		public TextColumnTemplate(ListItemType itemType)
		{
			this.ItemType = itemType;
		}
		public string DataField {
			get { return mDataField; }
			set { mDataField= value; }
		}
		public bool DesignMode {
			get { return mDesignMode; }
			set { mDesignMode= value; }
		}
		public ListItemType ItemType {
			get { return mItemType; }
			set { mItemType= value; }
		}
		public string Text {
			get { return mText; }
			set { mText= value; }
		}
		public Unit Width {
			get { return mWidth; }
			set { mWidth= value; }
		}
		private string GetValue(DataGridItem container)
		{
			string itemValue = Null.NullString;
			if (!String.IsNullOrEmpty(DataField)) {
				if (DesignMode) {
					itemValue = "DataBound to " + DataField;
				} else {
					if (container.DataItem != null) {
						object evaluation = DataBinder.Eval(container.DataItem, DataField);
						if ((evaluation != null)) {
							itemValue = evaluation.ToString();
						}
					}
				}
			}
			return itemValue;
		}
		private void Item_DataBinding(object sender, System.EventArgs e)
		{
			DataGridItem container;
			int keyValue = Null.NullInteger;
			switch (ItemType) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:
					Label lblText = (Label)sender;
					container = (DataGridItem)lblText.NamingContainer;
					lblText.Text = GetValue(container);
					break;
				case ListItemType.EditItem:
					TextBox txtText = (TextBox)sender;
					container = (DataGridItem)txtText.NamingContainer;
					txtText.Text = GetValue(container);
					break;
			}
		}
		public void InstantiateIn(Control container)
		{
			switch (ItemType) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:
					Label lblText = new Label();
					lblText.Width = Width;
					lblText.DataBinding += Item_DataBinding;
					container.Controls.Add(lblText);
					break;
				case ListItemType.EditItem:
					TextBox txtText = new TextBox();
					txtText.Width = Width;
					txtText.DataBinding += Item_DataBinding;
					container.Controls.Add(txtText);
					break;
				case ListItemType.Footer:
				case ListItemType.Header:
					container.Controls.Add(new LiteralControl(Text));
					break;
			}
		}
	}
}
